<script>
import axios from "axios";
import global from "@/views/global/global";
export default {
  name: "Reserve",
  data() {
    return {
      startTime:'',
      endTime:'',
      realName:global.user.realName,
      pageDto:{
        pageNum:1,
        pageSize:3,
        data:{
          startTime:'',
          endTime:''
        }
      },
      reserveDto:{
        startTime:'',
        endTime:'',
        userId:global.user.id,
        reason:'',
        roomIds:[]
      },
      total:0,
      pages:0,
      roomList:[],
    }
  },
  methods:{
    // 预定
    reserve(){
      this.reserveDto.startTime=this.startTime;
      this.reserveDto.endTime=this.endTime;
      if (this.startTime==='' || this.endTime==='')
      {
        alert("请选择预定时间")
        return;
      }
      if (this.reserveDto.roomIds.length===0){
        alert("请选择要预定的会议室")
        return;
      }
      if (this.reserveDto.reason===''){
        alert("请输入预定原因")
        return;
      }
        axios.post("/oa/room/reserve",this.reserveDto).then((res)=>{
          if (res.data.code===1){
            this.$message({
              type: 'success',
              message: '预定成功!'
            });
            this.reserveDto.roomIds=[];
            this.reason='';
            this.submitForm();
          }
        })
    },
    //查询
    submitForm(){
      this.pageDto.data.startTime=this.startTime;
      this.pageDto.data.endTime=this.endTime;
      axios.post("/oa/room/getRoomByTime",this.pageDto).then((res)=>{
        this.pageDto.pageNum=res.data.data.pageNum;
        this.pageDto.pageSize = res.data.data.pageSize;
        this.total=res.data.data.total;
        this.pages=res.data.data.pages;
        this.roomList=res.data.data.list;
      })
    },
    handleSelectionChange(val){
      this.reserveDto.roomIds = val
      this.reserveDto.roomIds = val.map(item=>item.roomId)
    },
  },
  created() {
    this.submitForm();
  }
}
</script>

<template>
  <div class="titleBox">
    会议室预定
  </div>

  <div>
    <div class="block" >
      <el-date-picker
          v-model="this.startTime"
          type="datetime"
          placeholder="预定开始时间"
      />
      <span class="demonstration">To</span>
      <el-date-picker @change="submitForm()"
          v-model="this.endTime"
          type="datetime"
          placeholder="预定结束时间"
      />
    </div>
  </div>


  <div>
    <!-- 首页表格信息显示 -->
    <el-table :data="this.roomList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column  type="selection" width="50"></el-table-column>
      <el-table-column label="会议室名称">
        <template #default="scope">
          <div style="display: flex; align-items: center">

            <span style="margin-left: 10px">{{ scope.row.roomName}}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="会议室描述">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <span style="margin-left: 10px">{{ scope.row.description }}</span>
          </div>
        </template>
      </el-table-column>


    </el-table>


    <!-- 分页-->
    <el-pagination
        v-model:current-page="this.pageDto.pageNum"
        :page-size="this.pageDto.pageSize"
        :pager-count="this.pages"
        layout="prev, pager, next"
        :total="this.total"
        @current-change="submitForm"
    />
    预定人: <el-input v-model="this.realName"  style="max-width: 150px" disabled>{{this.realName}}</el-input>
    预定原因: <el-input v-model="this.reserveDto.reason" style="max-width: 150px" placeholder="请输入预定原因"> </el-input>
    <el-button type="primary" @click="reserve()">预定</el-button>
  </div>
</template>

<style scoped lang="less">

</style>
